<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
				<el-form-item label="上级分类" prop="pid">
					<el-select class="flex-1" v-model="formData.pid" clearable placeholder="请选择分类">
						<el-option v-for="(item, index) in categoryList.list" :key="index" :label="item.cate_name"	:value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item label="分类名称" prop="cate_name">
					<el-input v-model="formData.cate_name" clearable placeholder="请输入分类名称" />
				</el-form-item>
				<el-form-item label="分类图标" prop="pic">
					<material-picker v-model="formData.pic" />
				</el-form-item>
				<el-form-item label="分类大图" prop="big_pic">
					<material-picker v-model="formData.big_pic" />
				</el-form-item>
				<el-form-item label="icon图标" prop="big_pic">
					<icon-picker v-model="formData.icon" />
				</el-form-item>
				<el-form-item label="显示" required prop="is_show">
					<el-radio-group v-model="formData.is_show">
						<el-radio :label="1">是</el-radio>
						<el-radio :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="排序" >
					<el-input v-model="formData.sort" clearable placeholder="请输入排序" />
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup name="guideCategoryEdit">
	import type { FormInstance } from 'element-plus'
	import Popup from '@/components/popup/index.vue'
	import { apiGuideCategoryAdd, apiGuideCategoryEdit, apiGuideCategoryDetail,apiGuideCategoryClassifyList } from '@/api/guide_category'
	import { timeFormat } from '@/utils/util'
	import type { PropType } from 'vue'
	// defineProps({
	// 	dictData: {
	// 		type: Object as PropType<Record<string, any[]>>,
	// 		default: () => ({})
	// 	}
	// })
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')
	
	const categoryList = reactive({
		list:[{
			id:0,
			cate_name:'顶级分类'
		}]
	})

	// 弹窗标题
	const popupTitle = computed(() => {
		return mode.value == 'edit' ? '编辑导航分类' : '新增导航分类'
	})

	// 表单数据
	const formData = reactive({
		id: '',
		pid: '',
		cate_name: '',
		sort: 0,
		pic: '',
		is_show: 1,
		big_pic: '',
		icon:''
	})


	// 表单验证
	const formRules = reactive<any>({
		pid: [{
			required: true,
			message: '请选择父id',
			trigger: ['blur']
		}],
		cate_name: [{
			required: true,
			message: '请输入分类名称',
			trigger: ['blur']
		}],
	})


	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}


	}
	
	const getClasslist = async () =>{
		const Classlist = await apiGuideCategoryClassifyList()
		let arr = [{
			id:0,
			cate_name:'顶级分类'
		}]
		Classlist.forEach(function (item:any){
			arr.push(item)
		})
		categoryList.list = arr;
		
	}

	const getDetail = async (row : Record<string, any>) => {
		const data = await apiGuideCategoryDetail({
			id: row.id
		})
		setFormData(data)
	}


	// 提交按钮
	const handleSubmit = async () => {
		await formRef.value?.validate()
		const data = { ...formData, }
		mode.value == 'edit'
			? await apiGuideCategoryEdit(data)
			: await apiGuideCategoryAdd(data)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}

	getClasslist()

	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>